package com.springapp.dao.dao.impl;

import com.springapp.dao.dao.ILigneCommandeDAO;
import com.springapp.model.LigneBon_Preparation;
import com.springapp.model.LigneCommande;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thomas on 30/08/14.
 */
@Repository
public class LigneBonPrepaDAO {
    @Qualifier("dataSource") //Fait référence au datasource dans le mvc-dispatcher.xml
    @Autowired
    private DataSource dataSource;


    public List<LigneBon_Preparation> getListLigneBon(int _IDBonPrepa) {
        String sql="SELECT lbon.IDLigneBon, bon.IDCommande,lbon.IDArticle, lbon.IDEmplacement,lbon.IDConditionnement,lbon.IDPalette,QteArticle,lbon.IDBonPreparation\n" +
                "FROM LigneBon_Prepa lcmd\n" +
                "  INNER JOIN BonPreparation bon ON lbon.IDBonPreparation=bon.IDBonPreparation\n" +
                "  INNER JOIN Article art ON lbon.IDArticle= art.IDArticle\n" +
                "WHERE bon.IDBonPreparation='"+_IDBonPrepa+"'";


        Connection conn=null;
        List<LigneBon_Preparation> lstLigneBon=new ArrayList<LigneBon_Preparation>();

        try {
            conn=dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet result=ps.executeQuery();
            while(result.next()){
                LigneBon_Preparation ligneBonprepa=new LigneBon_Preparation(
                        result.getInt("IDLigneBon"),
                        result.getInt("IDArticle"),
                        result.getInt("IDEmplacement"),
                        result.getInt("IDConditionnement"),
                        result.getInt("IDPalette"),
                        result.getInt("NbArticle"),
                        result.getInt("NbPalette"),
                        result.getInt("IDBonPreparation")
                );
                lstLigneBon.add(ligneBonprepa);
            }
            result.close();
            ps.close();


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lstLigneBon;

    }



    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
